package com.guanyong.services;

import com.guanyong.dao.IUserBaseDaoMapper;
import com.guanyong.dao.IUserInfoDaoMapper;
import com.guanyong.model.UserBaseModel;
import com.guanyong.model.UserInfoModel;
import com.guanyong.utils.Cache.ICahce.ICache;
import com.guanyong.utils.Comm.ResultCode;
import com.guanyong.utils.Comm.ResultObject;
import com.guanyong.utils.Encrypt.EncryptExt;
import com.guanyong.utils.UniqueID.DBObjectID;
import org.apache.ibatis.annotations.ResultMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.HashMap;
import java.util.List;

/**
 * Created by mxk on 2017/5/29.
 */
@Service
public class UserInfoService {


    @Autowired
    private IUserInfoDaoMapper iUserInfoDaoMapper;


    @Autowired
    UserBaseService userBaseService;


    @Autowired
    IUserBaseDaoMapper userBaseDao;


    @Autowired
    ICache cache;

    @Value("${mortise.usercas.model.cacheKey}")
    String casModelCacheKey;

    public HashMap<String,Object> GetUserList(UserBaseModel userBaseModel,
                                              Integer isvip,
                                              String btime,
                                              String etime,
                                              Integer pno,
                                              Integer pagesize,
                                              Integer allcount){
        HashMap<String,Object> hashMap=new HashMap();
        if (allcount<0){
            Integer count=iUserInfoDaoMapper
                    .SelectUserListCount(userBaseModel,isvip,btime,etime);
            hashMap.put("count",count);
        }
        else {
            hashMap.put("count",allcount);
        }
        List<UserInfoModel> userInfoModelList
                =iUserInfoDaoMapper.SelectUserList(userBaseModel,isvip,btime,etime,(pno-1)*pagesize,
                pagesize);
        hashMap.put("list",userInfoModelList);
        return hashMap;
    }

    public UserInfoModel GetUserInfoModelById(String id){

        return iUserInfoDaoMapper.selectByPrimaryKey(id);
    }
    public List<UserInfoModel> GetUserInfoModelByIdList(String idlist){

        return iUserInfoDaoMapper.selectUserInfoByIdList(idlist);
    }

    public Integer UpdateUserinfo(UserInfoModel userInfoModel)
    {
        return  iUserInfoDaoMapper.update(userInfoModel);
    }

    //用户注册
    @Transactional
    public ResultObject<UserBaseModel> RegUser(String userPhone,
                                               String userPwd,
                                               String userPhoto,
                                               String userName,
                                               String qqOpenId,
                                               String wechatOpenid,
                                               String weibOpenid,
                                               String sex,
                                               Integer regapp) {

        ResultObject<UserBaseModel> resultObject = new ResultObject<UserBaseModel>();
        if (userPwd == null || userPwd.equals("")) {
            resultObject.setCode(ResultCode.ParamsError);
            resultObject.setErrorMsg("用户密码参数错误");
        }

        if (userPhone != null && !userPhone.equals("")) {
            if (userBaseService.isPhone(userPhone)) {
                UserBaseModel _user = userBaseService.getUserModel(userPhone,2);
                if (_user!=null) {
                    resultObject.setCode(ResultCode.FiledRepeat);
                    resultObject.setErrorMsg("手机已存在");
                }
            } else {
                resultObject.setCode(ResultCode.ParamsError);
                resultObject.setErrorMsg("手机号式错误");
            }

        }
        else{
            resultObject.setCode(ResultCode.ParamsError);
            resultObject.setErrorMsg("用户手机号参数错误");
        }

        if (resultObject.getErrorMsg().length() == 0) {
            if (resultObject.getErrorMsg() == null || resultObject.getErrorMsg().equals("")) {
                //调用添加；
                String newuserid = new DBObjectID().toString();
                if (newuserid != null && !newuserid.equals("")) {
                    UserBaseModel newUserModel=new UserBaseModel();
                    newUserModel.setfId(newuserid);
                    if (userPwd!=null && !userPwd.equals("")){
                        String passwordMd5 = EncryptExt.GetMd5Code(newuserid + userPwd);
                        newUserModel.setfPassword(passwordMd5);
                    }
                    else{
                        newUserModel.setfPassword("");
                    }
                    newUserModel.setfUsername(userName);
                    newUserModel.setfPhone(userPhone);
                    newUserModel.setfRole(0);
                    newUserModel.setfPhoto(userPhoto);
                    //填写默认值
                    newUserModel.setfEmail("");
                    newUserModel.setfState(0);
                    newUserModel.setfRegapp(regapp);
                    newUserModel.setfQqopenid(qqOpenId);
                    newUserModel.setfWechatopenid(wechatOpenid);
                    newUserModel.setfWeibopenid(weibOpenid);
                    int res = userBaseDao.insert(newUserModel);
                    if (res > -1) {
                        UserInfoModel userInfoModel=new UserInfoModel();
                        userInfoModel.setfId(newuserid);
                        userInfoModel.setfSex(sex);
                        Integer res2=iUserInfoDaoMapper.insert(userInfoModel);
                        if (res2>-1)
                        {
                            resultObject.setCode(ResultCode.OperateOk);
                            resultObject.setResult(newUserModel);
                            cache.put(casModelCacheKey, newUserModel.getfId(), newUserModel);
                            //写入cookie

                        }
                        else{
                            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                            resultObject.setCode(ResultCode.OperateFail);
                            resultObject.setErrorMsg("添加用详细信息失败");
                        }

                    } else {
                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                        resultObject.setCode(ResultCode.OperateFail);
                        resultObject.setErrorMsg("添加用户失败");
                    }
                } else {
                    resultObject.setCode(ResultCode.ServiceError);
                    resultObject.setErrorMsg("生成用户id报错");
                }
            }
        }
        return resultObject;
    }



    public ResultObject<UserBaseModel> RegUserOpenid(
            String userPhoto,
            String userName,
            String openid,
            Integer opentype,
            String  sex,
            Integer regapp) {

        ResultObject<UserBaseModel> resultObject = new ResultObject<UserBaseModel>();



        if (resultObject.getErrorMsg().length() == 0) {
            if (resultObject.getErrorMsg() == null || resultObject.getErrorMsg().equals("")) {
                //调用添加；
                String newuserid = new DBObjectID().toString();
                if (newuserid != null && !newuserid.equals("")) {
                    UserBaseModel newUserModel=new UserBaseModel();
                    newUserModel.setfId(newuserid);

                    newUserModel.setfUsername(userName);
                    newUserModel.setfRole(0);
                    newUserModel.setfPhoto(userPhoto);
                    //填写默认值
                    newUserModel.setfEmail("");
                    newUserModel.setfState(0);
                    newUserModel.setfRegapp(regapp);
                    if (opentype==4){
                        newUserModel.setfQqopenid(openid);
                    }
                    else if(opentype==5){
                        newUserModel.setfWechatopenid(openid);
                    }
                    else if(opentype==6){
                        newUserModel.setfWeibopenid(openid);
                    }
                    int res = userBaseDao.insert(newUserModel);
                    if (res > -1) {
                        UserInfoModel userInfoModel=new UserInfoModel();
                        userInfoModel.setfId(newuserid);
                        userInfoModel.setfSex(sex);
                        Integer res2=iUserInfoDaoMapper.insert(userInfoModel);
                        if (res2>-1)
                        {
                            resultObject.setCode(ResultCode.OperateOk);
                            resultObject.setResult(newUserModel);
                            cache.put(casModelCacheKey, newUserModel.getfId(), newUserModel);
                            //写入cookie

                        }
                        else{
                            resultObject.setCode(ResultCode.OperateFail);
                            resultObject.setErrorMsg("添加用详细信息失败");
                        }

                    } else {
                        resultObject.setCode(ResultCode.OperateFail);
                        resultObject.setErrorMsg("添加用户失败");
                    }
                } else {
                    resultObject.setCode(ResultCode.ServiceError);
                    resultObject.setErrorMsg("生成用户id报错");
                }
            }
        }
        return resultObject;
    }
}
